package com.ajou.adonate.imap;

import java.io.File;

import javax.mail.*;
import javax.mail.internet.MimeBodyPart;

public class FileDownloader extends Thread{
	private Message m_Msg;
	
	public String DownloadFile(Message FileMsg)
	{
		String reName = null;
		m_Msg = FileMsg;
		try {
			Multipart MP =(Multipart)m_Msg.getContent();
			MimeBodyPart M = (MimeBodyPart)MP.getBodyPart(0);
			if(M.getFileName() != null)
			{
				reName = M.getFileName();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return reName;
		
	}
	
	public void run()
	{
		try {
			Multipart MP =(Multipart)m_Msg.getContent();
			for (int i = 0; i < MP.getCount(); i++) {
				MimeBodyPart M = (MimeBodyPart)MP.getBodyPart(i);
				if(M.getFileName() != null)
				{
					String Filename = M.getFileName();
					System.out.println("Download Start!! "+ "D:\\upload\\DownTemp\\"+ Filename);
					/*
					System.out.println("File ContentType >> " + M.getContentType());
					System.out.println("File Name >> " + M.getFileName());
					System.out.println("File Size >> " + M.getSize());
					System.out.println("File Disopoision >> " + M.getDisposition());
					*/
					File file = new File("D:\\upload\\DownTemp\\" + Filename);
					M.saveFile(file);
					System.out.println("Download Success!! " + "D:\\upload\\DownTemp\\"+ Filename);
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
}
